import { Code, CodeError } from "mvx-foundation";
const request = async (options) => {
    return new Promise(resolve => {
        options.success = (result) => {
            resolve([result, null]);
        };
        options.fail = (err) => {
            resolve([undefined, new Error(err.errMsg)]);
        };
        uni.request(options);
    });
};
export class WXHttp {
    builder;
    enableRealtimeLog;
    async send() {
        let headers = {};
        this.builder.headers().forEach(((value, key) => {
            headers[key] = value;
        }));
        const url = this.builder.getUrl();
        console.log(`${url} req `, this.builder.content());
        if (this.enableRealtimeLog) {
            uni.getRealtimeLogManager().info([`${url} req `, this.builder.content()]);
        }
        const [ret, err] = await request({
            url: url,
            method: this.builder.method(),
            header: headers,
            data: this.builder.content(),
            dataType: "其他",
            responseType: "text",
        });
        console.log(`${url} res `, ret);
        if (this.enableRealtimeLog) {
            uni.getRealtimeLogManager().info([`${url} res `, ret]);
        }
        if (err) {
            return [JSON.stringify({}), new CodeError(err.message, Code.ElseError)];
        }
        return [ret?.data ?? JSON.stringify({}), new CodeError(`code is ${ret?.statusCode}`, ret?.statusCode)];
    }
    constructor(builder, enableRealtimeLog = false) {
        this.builder = builder;
        this.enableRealtimeLog = enableRealtimeLog;
    }
}
